import axios from 'axios'
import {Message} from '@arco-design/web-vue'

//请求拦截器
axios.interceptors.request.use(config => {
    let token = window.localStorage.getItem("token");
    //如果存在token,请求就携带
    if (token) {
        config.headers['Authorization'] = token
    }
    return config
}, error => {
    console.log('request:', error)
})

//相应拦截器
axios.interceptors.response.use(success => {
    //请求是否成功
    if (success.status && success.status === 200) {
        //请求业务逻辑是否正确
        if (success.data.code === 200) {
            if (success.config.method === 'get') {
                for (let key in url) {
                    if (url[key].url === success.config.url) {
                        if (url[key].needKey) {
                            convertData(success.data.obj)
                            break
                        }
                    }
                }
            }
            return success.data
        } else {
            //业务逻辑错误
            if (success.data.obj) {
                Message.error(success.data.message + ':' + success.data.obj)
            } else {
                Message.error(success.data.message)
            }
            if (success.data.code === 500214) {
                window.localStorage.removeItem("token")
                window.localStorage.removeItem("user")
            }
            return
        }
    } else {
        Message.error("请求返回状态无效")
    }
    return
}, error => {
    Message.error("请求失败")
    return
})
export const convertData = (obj) => {
    if (Array.isArray(obj)) {
        for (let item of obj) {
            item.key = item.id
            item.title = item.name
            if (item.children && Array.isArray(item.children) && item.children.length > 0) {
                item.checkable = false
                convertData(item.children)
            }
        }
    } else {
        obj.key = obj.id
        obj.title = obj.name
        if (obj.children && Array.isArray(obj.children) && obj.children.length > 0) {
            convertData(obj.children)
        }
    }
}
let base = '';
//传送JSON格式的POST
export const postRequest = (url, data) => {
    return axios({
        method: 'POST',
        url: `${base}${url}`,
        data: data
    })
}
export const putRequest = (url, data) => {
    return axios({
        method: 'PUT',
        url: `${base}${url}`,
        data: data
    })
}

export const getRequest = (url, params) => {
    return axios({
        method: 'GET',
        url: `${base}${url}`,
        params: params
    })
}

export const deleteRequest = (url, data) => {
    return axios({
        method: 'DELETE',
        url: `${base}${url}`,
        data: data
    })
}

export const url = {
    captcha: {
        url: '/captcha/getCaptcha',
        des: '获取验证码image流'
    },
    doLogin: {
        url: '/login/doLogin',
        des: '登录获取token'
    },
    getAdminInfo: {
        url: '/login/getUserInfo',
        des: '获取当前登录用户'
    },
    logout: {
        url: '/login/doLogout',
        des: '退出登录'
    },
    getMenuByAdminId: {
        url: '/system/cfg/menu',
        des: '通过用户ID查询菜单列表'
    },
    getAllPositions: {
        url: '/system/basic/pos/getAllPositions',
        des: '获取所有职位信息',
        needKey: true
    },
    addPos: {
        url: '/system/basic/pos/',
        des: '添加职位信息'
    },
    delPos: {
        url: '/system/basic/pos/',
        des: '删除职位信息'
    },
    delMorePos: {
        url: '/system/basic/pos/',
        des: '批量删除职位信息'
    },
    editPos: {
        url: '/system/basic/pos/',
        des: '修改职位信息'
    },
    getAllJoblevels: {
        url: '/system/basic/joblevel/getAllJoblevels',
        des: '获取所有职称信息',
        needKey: true
    },
    addJoblevel: {
        url: '/system/basic/joblevel/',
        des: '添加职称信息'
    },
    editJoblevel: {
        url: '/system/basic/joblevel/',
        des: '修改职称信息'
    },
    deleteJoblevel: {
        url: '/system/basic/joblevel/',
        des: '删除职称信息'
    },
    deleteMoreJoblevel: {
        url: '/system/basic/joblevel/',
        des: '批量删除职称信息'
    },
    addRole: {
        url: '/system/basic/permiss/',
        des: '添加角色信息'
    },
    editRole: {
        url: '/system/basic/permiss/',
        des: '修改角色信息'
    },
    getAllRoles: {
        url: '/system/basic/permiss/getAllRoles',
        des: '获取所有角色信息',
        needKey: true
    },
    delRole: {
        url: '/system/basic/permiss/',
        des: '删除角色'
    },
    delMoreRole: {
        url: '/system/basic/permiss/',
        des: '批量删除角色'
    },
    getAllMenus: {
        url: '/system/basic/permiss/menus',
        des: '查询所有菜单',
        needKey: true
    },
    getMenusByRoleId: {
        url: '/system/basic/permiss/mid/',
        des: '根据角色ID查询菜单ID',
        needKey: true
    },
    updateRoleMenus: {
        url: '/system/basic/permiss/updateMenuRoles',
        des: '更新角色菜单'
    },
    getAllDepartments: {
        url: '/system/basic/department/',
        des: '获取所有部门',
        needKey: true
    },
    addDepartment: {
        url: '/system/basic/department/',
        des: '添加部门'
    },
    editDepartment: {
        url: '/system/basic/department/',
        des: '修改部门'
    },
    delDepartment: {
        url: '/system/basic/department/',
        des: '删除部门'
    },
    getAllAdmins: {
        url: '/system/admin/',
        des: '获取所有操作员',
        needKey: true
    },
    getAllRolesInAdmin: {
        url: '/system/admin/roles',
        des: '获取所有角色'
    },
    updateAdmin: {
        url: '/system/admin/',
        des: '更新操作员'
    },
    deleteAdmin: {
        url: '/system/admin/',
        des: '删除操作员'
    },
    updateAdminRoles: {
        url: '/system/admin/updateAdminRoles',
        des: '删除操作员'
    },
    getAllEmps: {
        url: '/employee/basic/',
        des: '查询所有员工并分页',
        needKey: true
    },
    addEmp: {
        url: '/employee/basic/',
        des: '添加员工'
    },
    updateEmp: {
        url: '/employee/basic/',
        des: '修改员工'
    },
    delEmp: {
        url: '/employee/basic/',
        des: '删除员工'
    },
    getAllDepartmentsInEmp: {
        url: '/employee/basic/departments',
        des: '获取所有部门',
        needKey: true
    },
    getAllJoblevelsInEmp: {
        url: '/employee/basic/joblevels',
        des: '获取所有职称',
        needKey: true
    },
    getMaxWorkId: {
        url: '/employee/basic/maxWorkId',
        des: '获取工号'
    },
    getAllNations: {
        url: '/employee/basic/nations',
        des: '获取所有民族',
        needKey: true
    },
    getPoliticsStatus: {
        url: '/employee/basic/politicsStatus',
        des: '获取所有政治面貌',
        needKey: true
    },
    getPositions: {
        url: '/employee/basic/positions',
        des: '获取所有职位',
        needKey: true
    },
    exportEmp: {
        url: '/employee/basic/export',
        des: '导出员工数据'
    },
    importEmp: {
        url: '/employee/basic/import',
        des: '导入员工数据'
    },
    getAllSalarys: {
        url: '/salary/sob/',
        des: '获取所有账套',
        needKey: true
    },
    addSalary: {
        url: '/salary/sob/',
        des: '添加账套'
    },
    editSalary: {
        url: '/salary/sob/',
        des: '修改账套'
    },
    delSalary: {
        url: '/salary/sob/',
        des: '删除账套'
    },
    getAllAdminsInChat: {
        url: '/chat/',
        des: '聊天获取操作员'
    },
    editAdminInfo: {
        url: '/admin/info',
        des: '修改当期登录用户信息'
    },
    editPassword: {
        url: '/admin/pass',
        des: '修改当期登录用户密码'
    },
    editUserFace: {
        url: '/admin/userFace',
        des: '修改当期登录用户头像'
    },
}
